import java.util.HashMap;

public class Test05 {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer,Integer> preSum = new HashMap<>();
        preSum.put(0,1);
        int len = nums.length;
        int res = 0;
        int sum_i = 0;
        for (int i = 0; i < len; i++) {
            sum_i += nums[i];
            int sum_j = sum_i - k;
            if(preSum.containsKey(sum_j)){
                res += preSum.get(sum_j);
            }
            preSum.put(sum_i,preSum.getOrDefault(sum_i,0) + 1);
        }
        return res;
    }
}
